<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vuex</title>
  </head>
  <body>
    <div id="app">
        <p>{{count}}</p>
        <p>{{doubleCount}}</p>
        <button @click="increase">+</button>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script src="vuex.js"></script>
    <script>
      Vue.use(Vuex);
      var store = new Vuex.Store({
        state: { count: 1 },
        mutations:{
            increase(state){
                state.count++;
            }
        },
        getters:{ 
            //定义一个访问器,它会返回count的2倍 store.getters.doubleCount
            doubleCount(state){
                return state.count*2;
            }
        }
      });
      var vm = new Vue({
        store,
        el: "#app",
        computed: {
          count() {
            return this.$store.state.count;
          },
          doubleCount() {
            return this.$store.getters.doubleCount;
          },
        },
        methods:{
            increase(){
                this.$store.commit('increase');
            }
        }
      });
    </script>
  </body>
</html>
